package efr.util;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;
 
public class SendMailTLS {
	private static Logger logger = Logger.getLogger(SendMailTLS.class);
 
	public static void sendMail() {
 
		final String username = "efrumos.ro@gmail.com";
		final String password = "124905598328";
 
		Properties props = new Properties();
		props.put("mail.smtp.user", "botez.nicolae@gmail.com");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "465");
		props.put("mail.smtp.starttls.enable","true");
		props.put("mail.smtp.debug", "true");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.fallback", "false");
 
		Session session = Session.getInstance(props,
		  new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		  });
		session.setDebug(true);
 
		try {
 
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("efrumos.ro@gmail.com"));
			message.setRecipients(Message.RecipientType.TO,
				InternetAddress.parse("elenadaniela.botez@gmail.com"));
			message.setSubject("e frumos");
			message.setText("e frumos!");
 
			Transport transport = session.getTransport("smtps");
			transport.connect("smtp.gmail.com", 465, "efrumos.ro", "124905598328");
			
			Transport.send(message, message.getAllRecipients());
			transport.close();
 
			logger.debug("Done");
 
		} catch (MessagingException e) {
			logger.error("Error: ", e);
			throw new RuntimeException(e);
		}
	}
}